//
//  BleListViewController.m
//  MMWMiotLinkDemo
//
//  Created by 程杰 on 2020/12/31.
//  Copyright © 2020 HGG. All rights reserved.
//

#import "BleListViewController.h"
//#import "MLBLEModel.h"
#import "Masonry.h"
#import "MLBLEManager.h"
//#import "WQMLBleDeviceInfo.h"
#import "BleViewController.h"

@interface BleListViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) NSMutableArray <MLBLEModel *> *dataList;
@property (nonatomic,strong) NSMutableArray <CBPeripheral *> * periperals;

@end

@implementation BleListViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
        
    [[MLBLEManager sharedManager] mlBleStopScan];
    
    if (self.dataList.count > 0) {
        [self.dataList removeAllObjects];
    }
    
    if (self.periperals.count > 0) {
        [self.periperals removeAllObjects];
    }
    
    [self.tableView reloadData];
    
    [[MLBLEManager sharedManager] mlBleScan];


}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    
}
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];

    [[MLBLEManager sharedManager] mlBleStopScan];
//    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"蓝牙列表";

    [self tableView];
    [[MLBLEManager sharedManager] mlBLeRun];
    
    [[MLBLEManager sharedManager] isGetDeviceName:YES];
    
    __weak typeof(self) weakself = self;
//    [[MLBLEManager sharedManager] setDiscoverPeripheralBlock:^(MLBLEModel * _Nonnull model) {
//
//        if (model.name != nil && model.name > 0) {
//            MLBLEModel *item = [[MLBLEManager sharedManager] getBleInfoWithBroadcast:model];
//            if (item.isMLble) {
//                [weakself changeTableView:item];
//            }
//        }
//    }];
    
    [[MLBLEManager sharedManager] mlBleSearchCallBack:^(MLBLEModel * _Nonnull model) {
        
        if (model.name != nil && model.name > 0) {
//            NSLog(@"mode name:%@",model.name);
            MLBLEModel *item = [[MLBLEManager sharedManager] getBleInfoWithBroadcast:model];
            if (item.isMLble) {
                [weakself changeTableView:item];
            }
        }
    }];
    
}

- (void)changeTableView:(MLBLEModel *)model {

    if (![self.periperals containsObject:model.peripheral]) { // 不存在添加
//        MLBLEModel *item1 = [[MLBLEManager sharedManager] getBleInfoWithBroadcast:model];
        [self.periperals addObject:model.peripheral];
        [self.dataList addObject:model];
        [self.tableView reloadData];
        
    }
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSString *cellID = @"mycellID";
    
    MMWSearchBlueVCCell * cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    
    if (cell == nil) {
        cell = [[MMWSearchBlueVCCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    
    if(self.dataList.count > 0){
        MLBLEModel *model = self.dataList[indexPath.row];
        NSString *name = [NSString stringWithFormat:@"%@_%@",model.name,model.mac];
        cell.nameLb.text = name;
    }

    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    if (self.dataList.count > 0) {

    [[MLBLEManager sharedManager] mlBleStopScan];
        
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    BleViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"bleViewID"];
    vc.model = self.dataList[indexPath.row];
    [self.navigationController pushViewController:vc animated:YES];
    }
}

#pragma mark - lazy

- (NSMutableArray<CBPeripheral *> *)periperals {
    
    if (_periperals == nil) {
        _periperals = [NSMutableArray array];
    }
    return _periperals;
}

- (NSMutableArray<MLBLEModel *> *)dataList {
    
    if (_dataList == nil) {
        _dataList = [NSMutableArray array];
    }
    return _dataList;
}

- (UITableView *)tableView {
    
    if (_tableView == nil) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.tableFooterView = [UIView new];
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.rowHeight = 60;
        [self.view addSubview:_tableView];
        [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.view);
            make.left.right.equalTo(self.view);
            if (@available(iOS 11.0, *)) {
                make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom);
            } else {
                make.bottom.equalTo(self.view);
            }
        }];
    }
    return _tableView;
}


@end


@implementation MMWSearchBlueVCCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self.contentView addSubview:self.iconImgV];
        [self.contentView addSubview:self.nameLb];
    }
    return self;
}

- (UILabel *)nameLb {
    
    if (_nameLb == nil) {
        _nameLb = [UILabel new];
        _nameLb.numberOfLines = 0;

    }
    return _nameLb;
}

- (UIImageView *)iconImgV {
    
    if (_iconImgV == nil) {
        _iconImgV = [UIImageView new];
        _iconImgV.image = [UIImage imageNamed:@"ble_icon"];
    }
    return _iconImgV;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    [self.iconImgV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView).offset(15);
        make.centerY.equalTo(self.contentView);
        make.width.height.mas_equalTo(50);
    }];
    
    [self.nameLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.iconImgV.mas_right).offset(20);
        make.centerY.equalTo(self.contentView);
        make.right.equalTo(self.contentView).offset(-20);
    }];
}

@end
